import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Switch, Redirect } from 'react-router-dom';
import { observer, inject } from "mobx-react";
import DistributionCreate from './DistributionCreate/DistributionCreate';
import Distribution from './Distribution/Distribution';

@inject('rootStore') @observer
class IntegrationRouter extends Component {
  requireIntegration = (Layout, props) => {
    const integrationStore = this.props.rootStore.integrationStore;
    if (!integrationStore.templateLoadSuccess) {
      integrationStore.toGetTemplate()
    }
    if (integrationStore.isIntegrationCreate) {
      return <Redirect to="/integration/create" />
    } else return <Layout {...props} />
  }

  requireIntegrationCreate = (Layout, props ) => {
    const integrationStore = this.props.rootStore.integrationStore;
    if (!integrationStore.templateLoadSuccess) {
      integrationStore.toGetTemplate()
    }
    if (!integrationStore.isIntegrationCreate) {
      return <Redirect to="/integration/distribution" />
    } else return <Layout {...props} />
  }

  render() {
    return (
      <Router>
        <Switch>
          <Route path="/integration/distribution" exact component={ props => this.requireIntegration(Distribution, props) } />
          <Route path="/integration/create" component={ props => this.requireIntegrationCreate(DistributionCreate, props) } />
        </Switch>
      </Router>
    )
  }
}

export default IntegrationRouter;
